var listObj = (function ($) {
	var sourceData,perInfo;
	var pageType = '', pageData = {},statusKey=''
	var start = 0,Limit = 20;
	var pullDom;
	var pulling = false;
	var nurseId='',perId=''
	$.init();
	$.plusReady(function() {
		baseSetup();
	})
	if (!mui.os.plus) {
		baseSetup()
	}
	function getKyData(){
		if (pulling) {
			pullDom.endPullUpToRefresh()
			return;
		}
		pulling = true
		//parr 病区Id^开始日期^结束日期^论文题()
		var pdict = {'start':start,'limit':Limit,'parr':'^'+$('#stBtn')[0].innerText+'^'+$('#endBtn')[0].innerText+'^','nurseid':nurseId}
		var paramDict = {'method':pageData['method'],'params':JSON.stringify(pdict),'type':'RecQuery'}
		//console.log(JSON.stringify(paramDict))
		$.DHCWebService(paramDict, function(result) {
			//console.log(JSON.stringify(result))
			var dataArr = result.data
			if (!dataArr) {
				pullDom.endPullUpToRefresh();
				pulling = false
				return;
			}
			
			if (start == 0) {
				sourceData = dataArr;
			}else{
				sourceData = sourceData.concat(dataArr)
			}
			updateUI(dataArr)
			if (dataArr.length < Limit) {
				pullDom.endPullUpToRefresh(true);
			}else{
				pullDom.endPullUpToRefresh();
			}
			start += dataArr.length;
			pulling = false
		}, function(errorStr) {
		 	pullDom.endPullUpToRefresh();
		 	pulling = false
		});
	}
	function updateUI(dataArr){
		var titleStr = pageData['titleStr']
		var icon = pageData['icon']
		var UL = $('#UL')[0];
		var oldCount = UL.querySelectorAll('li').length;
		for(var i=0; i<dataArr.length; i++){
			var oneData = dataArr[i]
			var statusText = oneData[statusKey]
			var statusIcon = ''
			if (statusText == '已保存') {
				statusIcon = 'nur-statusS'
			}else if (statusText == '已提交') {
				statusIcon = 'nur-statusR'
			}else if (statusText == '已审核') {
				statusIcon = 'nur-statusA'
			}else if (statusText == '驳回'){
				statusIcon = 'nur-statusB'
			}
			var html = '<div class="liheader">'+
					'<img class="headerImg" src="../../dependence/wkplatform/images/'+icon+'.png"/>'+
					'<div class="headerText mui-ellipsis">'+oneData['CreatorName']+' '+titleStr+'</div>'+
					'<div class="headerTime">'+oneData['CreateDate']+'</div>'+
				'</div>'+getKyHtml(oneData)+
				'<div class="statusIcon"><div class="nurfont '+statusIcon+'"></div></div>'
			
			var newLi = document.createElement('li')
			newLi.setAttribute('class','mui-table-view-cell')
			newLi.innerHTML = html
			newLi.id = 'li-'+(oldCount+i);
			UL.appendChild(newLi)
			newLi.addEventListener('tap',tableCellTapped)
		}
	}
	function tableCellTapped(){
		var indexStr = this.id.split('-')[1]
		var index = parseInt(indexStr)
		var currData = sourceData[index]
		if (currData[statusKey] == '已保存'&&nurseId == currData['Creator']) {
			var pageName = getKyNewPageName(pageType)
			var webview = plus.webview.create(pageName+'.html', pageName, {
				background: '#fff',
				statusbar: {
					background: '#fff'
				}
			},{'pageType':pageType,'data':currData,'perInfo':perInfo});
			webview.show('fade-in');
			return
		}
		var keys = {'titleStr':pageData['titleStr'],'pageArr':pageData['pageArr'],'applyer':'CreatorName','applyDate':'CreateDate'}
		if (pageType == '科室创新备案') {
			var arr = [{'title':'背景','key':'LocInnBackGround'},{'title':'创新优点','key':'LocInnAdvantage'},
				{'title':'创新方法','key':'LocInnMethod'},{'title':'应用效果','key':'LocInnEffect'},
				{'title':'创新前照片','key':'BfPic','type':'file'},{'title':'创新后照片','key':'AfPic','type':'file'}]
			keys['pageArr'] = keys['pageArr'].concat(arr)
		}
		var webview = plus.webview.create('kyDetail.html', 'kyDetail', {
			background: '#fff',
			statusbar: {
				background: '#fff'
			}
		},{'keys':keys,'data':currData,'pageType':pageType});
		webview.show('fade-in');
	}

	//发表论文备案
	function getKyHtml(dict){
		var htmls = ''
		var pageArr = pageData['pageArr']
		for (var i = 0; i < pageArr.length;i++) {
			var oneData = pageArr[i];
			var title = oneData['title']
			var keyStr = oneData['key']
			var keyType = oneData['type']
			var valueStr = dict[keyStr];
			var html = '<div class="oneline"><div class="leftText">'+title+'：</div><div class="rightText mui-ellipsis">'
			if (keyType == 'auth') {
				html += getAuthorHtml(valueStr)
			}else if (keyType == 'file') {
				html +='<div class="nurfont nur-fujian"></div>'
				html +=getFileHtml(valueStr)
			}else{
				html += valueStr
			}
			html +='</div></div>'
			htmls += html
		}
		return htmls
	}

	function baseSetup(){
		$('.mui-scroll-wrapper').scroll({
			bounce: true,
			indicators: false, //是否显示滚动条
		});
		perInfo = app.getState()
		nurseId = perInfo['loginID']
		perId = perInfo['loginPerID']
		var stBtn = $('#stBtn')[0]
		var endBtn = $('#endBtn')[0]
		var curMonth = getDeviceYearMonthStr();
		stBtn.innerText = curMonth +'-01'
		var currDict = getYMDWithStr(curMonth)
		endBtn.innerText = curMonth +'-' + getMonthMaxDay(currDict['year'], currDict['month'])
		stBtn.addEventListener('tap',dateBtnTapped)
		endBtn.addEventListener('tap',dateBtnTapped)
		var self = plus.webview.currentWebview()
		pageType = self.title
		$('.mui-title')[0].innerText = pageType
		pageData = getPageInitData(pageType)
		statusKey = getKyStatusKey(pageType)
		var scroll = document.getElementById('scroll');
		pullDom = $(scroll).pullToRefresh({
			up: {
				callback: function() {
					getKyData()
				}
			}
		});
		getKyData();
		//统计按钮
		mui('body').on('tap','#statBtn',function(){
			var webview = plus.webview.create('../platformStat.html', 'platformStat', {
				background: '#fff',
				statusbar: {
					background: '#fff'
				}
			},{'pageType':pageType,'title':pageData['titleStr'].replace('备案',''),'nurseid':nurseId,perId:perId});
			webview.show('fade-in');
		})
		//新建按钮
		mui('body').on('tap','#addBtn',function(){
			var pageName = getKyNewPageName(pageType)
			var webview = plus.webview.create(pageName+'.html', pageName, {
				background: '#fff',
				statusbar: {
					background: '#fff'
				}
			},{'pageType':pageType,'perInfo':perInfo});
			webview.show('fade-in');
		})
		
	}
	
	function dateBtnTapped(){
		var _self = this;
		plus.nativeUI.pickDate(function(e) {
			var d = e.date;
			var dateStr = d.getFullYear()+'-'
			if (d.getMonth() < 9) {
				dateStr += '0'
			}
			dateStr += (d.getMonth() + 1)+'-'
			if (d.getDate() < 10) {
				dateStr += '0'
			}
			dateStr += d.getDate()
			_self.innerText = dateStr;
			resetKyRefresh();
		}, function(e) {}, {});
	}
	
	var resetKyRefresh = function (){
		start = 0;
		sourceData = []
		mui('#UL li').each(function (index,element) {
			element.removeEventListener('tap',tableCellTapped)
		})
		$('#UL')[0].innerHTML = '';
		$('#wrapper').scroll().scrollTo(0,0,100);
		if (pullDom) {
			pullDom.refresh(true)
		}
		getKyData();
	}
	var exportObj = {}
	exportObj.resetKyRefresh = resetKyRefresh
	return exportObj
})(mui)

function detailChanged(){
	listObj.resetKyRefresh();
}

/* 发表论文备案
	{"ThesisTitle":"臧阳爽发表论文测试","ThesisName":"测试期刊","ThesisSpec":"Y","ThesisSpecDesc":"是","ThesisLevel":"73||1",
	"ThesisLevelDesc":"核心期刊","ThesisType":"72||3","ThesisTypeDesc":"论文集","ThesisPage":"2022,5：25","ThesisPublic":"2022-02-01",
	"ThesisStatus":"已保存","tableData":"F「臧阳爽「29「0,S「小花花「1「1","Creator":"0","ThesisAuthor":"臧阳爽","ThesisPart":"小花花",
	"CreatorName":"管理员","CreateDate":"2022-02-11","ThesisFile":"","rw":"3"}]
交流获奖论文
	{"AwardTitle":"臧阳爽测试交流获奖论文","AwardAuthor":"1,2","AwardAuthorName":"冯亮,张芮菲","AwardName":"测试会议",
	"AwardSpec":"N","AwardSpecDesc":"否","AwardLevel":"21||1","AwardLevelDesc":"国家级","AwardSituation":"38||1",
	"AwardSituationDesc":"特等奖","AwardPart":"4,5,6","AwardPartName":"程皓,周芷诺,周月绮","AwardDate":"2022-02-08",
	"AwardStatus":"N","AwardStatusDesc":"已保存","Creator":"0","CreatorName":"管理员","CreateDate":"2022-02-12","AwardFile":"","rw":"6"}
年底上交论文
	{"EytNurse":"2","EytNurseName":"张芮菲","EytLevel":"19||1","EytLevelDesc":"N0","EytHire":"12||3","EytHireDesc":"主管护师",
	"EytWard":"1","EytWardDesc":"CCU","EytTitle":"年底上交论文测试1","EytType":"72||9","EytTypeDesc":"学位论文","EytStatus":"N",
	"EytStatusDesc":"已保存","Creator":"0","CreatorName":"管理员","CreateDate":"2022-02-12","EytFile":"","rw":"3"}
科研立项
	{"RegSciAppDate":"2022-02-01","RegSciItemDate":"2022-02-08","RegSciType":"33||1","RegSciTypeDesc":"国家自然科学基金",
	"RegSciName":"科研立项测试","RegSciLead":"2","RegSciLeadName":"张芮菲","RegSciPart":"3,4","RegSciPartName":"周渔,程皓",
	"RegSciSituation":"已完成","RegSciStatus":"N","RegSciStatusDesc":"已保存","Creator":"0","CreatorName":"管理员",
	"CreateDate":"2022-02-12","rw":"2"}
科室创新备案
	{"LocInnType":"74||2","LocInnTypeDesc":"技术创新","LocInnTheme":"科室创新测试","LocInnWard":"2","LocInnWardDesc":"产房",
	"LocInnSpec":"Y","LocInnSpecDesc":"是","LocInnLead":"3","LocInnLeadName":"周渔","LocInnPart":"2,4","LocInnPartName":"张芮菲,程皓",
	"LocInnBackGround":"这是创新背景","LocInnAdvantage":"这是创新优点","BfPic":"","AfPic":"","LocInnMethod":"这是创新方法",
	"LocInnEffect":"这是应用效果","LocInnStatus":"N","LocInnStatusDesc":"已保存","Creator":"0","CreatorName":"管理员",
	"CreateDate":"2022-02-12","rw":"1"}
获奖创新备案
	{"AwaInnType":"74||1","AwaInnTypeDesc":"工具创新","AwaInnTheme":"获奖创新主题","AwaInnWard":"4","AwaInnWardDesc":"产科一区",
	"AwaInnSpec":"Y","AwaInnSpecDesc":"是","AwaInnLead":"4","AwaInnLeadName":"程皓","AwaInnPart":"5,6","AwaInnPartName":"周芷诺,周月绮",
	"AwaInnAward":"38||2","AwaInnAwardDesc":"一等奖","AwaPic":"","AwaInnLevel":"21||1","AwaInnLevelDesc":"国家级","AwaInnStatus":"N",
	"AwaInnStatusDesc":"已保存","Creator":"0","CreatorName":"管理员","CreateDate":"2022-02-12","rw":"1"}

专利备案
	{"PatentName":"专利名称","PatentType":"34||1","PatentTypeDesc":"国家发明专利","PatentMechanism":"认证机构","PatentNumber":"专利号",
	"PatentDate":"2022-02-11","PatentPer":"3,4,5","PatentPerName":"周渔,程皓,周芷诺","PatentPic":"","PatentWard":"5",
	"PatentWardDesc":"创伤骨科","PatentStatus":"N","PatentStatusDesc":"已保存","Creator":"0","CreatorName":"管理员",
	"CreateDate":"2022-02-12","rw":"1"}

已保存 nur-statusS
已提交 nur-statusR
已审核 nur-statusA
驳回 nur-statusB
*/